package com.zhanghaijun.createCode.service;

import com.zhanghaijun.createCode.ad.util.AdDoActionUtil;
import com.zhanghaijun.createCode.bean.AdSynchronousBean;
import com.zhanghaijun.createCode.bean.AdUser;
import com.zhanghaijun.createCode.util.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * Created by zhanghaijun on 15/5/6.
 */
public class ExcelImportService {

    /**
     * excel 导入 并且群发邮件
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String path = Constant.outPutRootPath + "3.xlsx";
        File file = new File(path);
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
        Workbook workbook = new XSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        int minRowIx = sheet.getFirstRowNum() + 1;
        int maxRowIx = sheet.getLastRowNum();
        FormulaEvaluator evaluator = workbook.getCreationHelper()
                .createFormulaEvaluator();

        for (int rowNum = minRowIx; rowNum <= maxRowIx; rowNum++) {
            AdUser adUser = new AdUser();
            Row row = sheet.getRow(rowNum);
            Cell cell1 = row.getCell(new Integer(1));//loginName
            CellValue cellValue1 = evaluator.evaluate(cell1);
            String userPrincipalName = cellValue1.getStringValue();
            adUser.setUserPrincipalName(userPrincipalName);

            Cell cell24 = row.getCell(new Integer(24));
            if (cell24 != null) {
                CellValue cellValue24 = evaluator.evaluate(cell24);
                if (cellValue24 != null) {
                    String mail = cellValue24.getStringValue();
                    adUser.setMail(mail);
                }
            }


            //初始化密码
            String pwd = StringUtil.getRandomString(8);
            String loginName = adUser.getUserPrincipalName();
            JsonResultDto res = AdDoActionUtil.changePassword(loginName, "", "a123456");

            if(res.isSuccess()){
                PrintUtil.println("loginName:" + loginName + "修改密码成功 初始密码是:a123456");
            }


//            if (res.isSuccess()) {
//                PrintUtil.println("loginName:" + loginName + "修改密码成功 初始密码是:" + pwd);
//                String content = "monitor系统升级 你的密码被重置,重置后的密码为:" + pwd;
//
//                String email = adUser.getMail();
//                String title = "由于monitor系统升级,你的密码被重置";
//                String url = SEND_MAIL_PATH + "?c=" + content + "&m=" + email + "&t=" + title;
//                PrintUtil.println(url);
//                try {
//                    String result = Jsoup.connect(url).ignoreContentType(true).timeout(30 * 1000).get().text();
//                    PrintUtil.println("loginName:" + loginName + "邮件发送状态" + result);
//                } catch (Exception e) {
//                    PrintUtil.println("loginName:" + loginName + "邮件失败");
//                    e.printStackTrace();
//                }
//                //发邮件
//            } else {
//                PrintUtil.println("loginName:" + loginName + "修改密码失败");
//            }
        }
    }

    private static final String SEND_MAIL_PATH = "http://192.168.59.104:2005/monitor/tool/mail.do";


}
